# -*- coding: utf-8 -*-
"""
 ----------------------------------------
|File Name:     field_json
|Author:        WYT
|date:          2020/8/6
 ----------------------------------------
|  Change Activity:
|                   2020/8/6:
-----------------------------------------
"""
from base.BaseMethod import BM
from util.g import g


def check_fields(remote_res, db_res, flag=True):
    try:
        # print("db_res: {}".format(db_res))
        # print("remote_res: {}".format(remote_res))
        if not remote_res and db_res is True:
            return True
        if type(remote_res) == list and type(db_res) == list:
            for index, i in enumerate(remote_res):
                # if i not in db_res:
                #     print("{} not in db_data".format(i))
                #     flag = False
                if len(db_res) < index + 1:
                    break
                flag = check_fields(remote_res=i, db_res=db_res[index])
        if type(remote_res) == dict and type(db_res) == dict:
            for k, v in remote_res.items():
                # print(flag)
                db_v = db_res.get(k)
                if type(v) == dict:
                    # print("dict_v: {} db_v: {}".format(v, db_v))
                    # print(db_res)
                    flag = check_fields(remote_res=v, db_res=db_v, flag=flag)
                elif type(v) == list:
                    # print(type(v))
                    # print(v)
                    print("db_v: {}".format(db_v))
                    if not db_v or type(db_v) == bool:
                        flag = True
                    else:
                        for index, va in enumerate(v):
                            # print(va, db_v)
                            # print(type(va), type(db_v))
                            flag = check_fields(remote_res=va, db_res=db_v[index])
                else:
                    # print(v, db_v)
                    if (type(v) == str and type(db_v) == str) and (v.isdigit() or v.replace(".", "").isdigit()):
                        if float(v) == float(db_v):
                            continue
                        else:
                            print("{} is not equal remote: {} db: {}".format(k, repr(v), repr(db_v)))
                            flag = False
                            continue
                    if type(v) == str and type(db_v) == str:
                        v = BM.handle_str(s=v)
                        db_v = BM.handle_str(s=db_v)
                        if v == db_v:
                            pass
                            continue
                        else:
                            print("{} is not equal remote: {} db: {}".format(k, repr(v), repr(db_v)))
                            flag = False
                            continue
                    if v == db_v or not all([v, db_v]):
                        pass
                    else:
                        print("{} is not equal remote: {} db: {}".format(k, repr(v), repr(db_v)))
                        flag = False
        return flag
    except Exception as e:
        msg = "error: {} file: {} line: {}".format(e, e.__traceback__.tb_frame.f_globals.get("__file__"),
                                                            e.__traceback__.tb_lineno)
        g.logger.error(msg)
        raise Exception(msg)

